package com.deeper.customview.ui.c6.exercise.c06e01;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

//TODO 只实现了margin, 没有实现垂直滚动
public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context) {
        super(context);
    }


    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(this.getContext(), attrs);
    }


    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }


    private int measureWidth(int widthMeasureSpec) {
        int width = 0;
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            int n = getChildCount();
            int totalWidth = 0;
            for (int i = 0; i < n; i++) {
                View child = getChildAt(i);
                int childWidth = child.getMeasuredWidth();
                //单个子组件宽度不能超过容器宽度
                if (childWidth > size) {
                    throw new IllegalStateException("Sub view is too large");
                }
                totalWidth += childWidth;
                if (totalWidth > size) {
                    width = size;
                    break;
                } else {
                    width = totalWidth;
                }
            }
        }
        //padding
        width += getPaddingLeft() + getPaddingRight();
        return width;
    }


    private int measureHeight(int heightMeasureSpec) {
        int height = 0;
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            int width = getMeasuredWidth();

            int n = getChildCount();
            int rowMaxHeight = 0;//当前行最大高度
            int rowWidth = 0; //当前行宽度
            for (int i = 0; i < n; i++) {
                View child = getChildAt(i);
                int childWidth = child.getMeasuredWidth();
                MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
                int childLeftMargin = layoutParams.leftMargin;
                int childTopMargin = layoutParams.topMargin;
                int childRightMargin = layoutParams.rightMargin;
                int childBottomMargin = layoutParams.bottomMargin;

                rowWidth += (childWidth + childLeftMargin + childRightMargin);
                if (rowWidth > width) {
                    //需要换行，当前子组件需要换到下一行
                    height += rowMaxHeight;

                    rowWidth = (childWidth + childLeftMargin + childRightMargin);
                    rowMaxHeight = (child.getMeasuredHeight() + childTopMargin + childBottomMargin);
                } else {
                    //不需要换行，可添加下一个子组件
                    rowMaxHeight = Math.max(rowMaxHeight,
                            (child.getMeasuredHeight() + childTopMargin + childBottomMargin));
                }
                //最后一个，要做特别处理
                if (i == n - 1) {
                    height += rowMaxHeight;
                }
            }
        }
        //padding
        height += getPaddingTop() + getPaddingBottom();
        return height;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int maxViewHeight = 0;//当前行的子组件的最大高度
        int maxLineWidth = 0;//当前行的子组件的总宽度
        int totalHeight = 0;//累计高度

        int n = getChildCount();
        int width = getMeasuredWidth();//容器宽度
        for (int i = 0; i < n; i++) {
            View child = getChildAt(i);
            MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
            int childLeftMargin = layoutParams.leftMargin;
            int childTopMargin = layoutParams.topMargin;
            int childRightMargin = layoutParams.rightMargin;
            int childBottomMargin = layoutParams.bottomMargin;

            //判断是否要换行显示(已占宽度+当前子组件宽度是否大于容器的宽度)
            if (maxLineWidth + child.getMeasuredWidth() + childLeftMargin + childRightMargin
                    > width - getPaddingLeft() - getPaddingRight()) {
                //换行后累加已显示的行的总高度
                totalHeight += maxViewHeight;
                //新起一行，新行的已占宽度和高度重置为0
                maxLineWidth = 0;
                maxViewHeight = 0;
            }
            //判断是否要换行后，放置该 child
            layoutChild(child, maxLineWidth, totalHeight,
                    maxLineWidth + child.getMeasuredWidth() + childLeftMargin,
                    totalHeight + child.getMeasuredHeight() + childTopMargin);
            //获取当前行的最高高度
            maxViewHeight = Math.max(maxViewHeight, child.getMeasuredHeight() + childTopMargin + childBottomMargin);
            //累加当前行的宽度
            maxLineWidth += (child.getMeasuredWidth() + childLeftMargin + childRightMargin);
        }
    }


    private void layoutChild(View child, int l, int t, int r, int b) {
        child.layout(l + getPaddingLeft(), t + getPaddingTop(),
                r + getPaddingRight(), b + getPaddingBottom());
    }
}
